package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.properties.WxCpProperties;
import com.ruoyi.system.domain.ProFile;
import com.ruoyi.system.domain.ProTask;
import com.ruoyi.system.domain.bo.ProSubjectBo;
import com.ruoyi.system.domain.bo.ProTaskBo;
import com.ruoyi.system.domain.vo.ProSubjectVo;
import com.ruoyi.system.domain.vo.ProTaskVo;
import com.ruoyi.system.mapper.ProFileMapper;
import com.ruoyi.system.mapper.ProTaskMapper;
import com.ruoyi.system.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 任务管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-10
 */
@RequiredArgsConstructor
@Service
public class ProFileServiceImpl implements IProFileService {

    private final ProFileMapper baseMapper;
    /**
     * 查询任务管理
     */
    @Override
    public ProFile queryById(Long taskId){
        ProFile proTaskVo = baseMapper.selectVoById(taskId);
        return proTaskVo;
    }

    /**
     * 查询任务管理列表
     */
    @Override
    public TableDataInfo<ProFile> queryPageList(ProFile bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProFile> lqw = buildQuery(bo);
        Page<ProFile> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务管理列表
     */
    @Override
    public List<ProFile> queryList(ProFile bo) {
        LambdaQueryWrapper<ProFile> lqw = buildQuery(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ProFile> buildQuery(ProFile bo) {
        String username = LoginHelper.getUsername();
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(bo.getVkey()), ProFile::getVkey, bo.getVkey());
        queryWrapper.eq(ObjectUtil.isNotNull(bo.getRefId()), ProFile::getRefId, bo.getRefId());
        return queryWrapper;
    }

    private LambdaQueryWrapper<ProTask> buildQueryWrapper(ProTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDdate() != null, ProTask::getDdate, bo.getDdate());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), ProTask::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getDesp()), ProTask::getDesp, bo.getDesp());
        lqw.eq(bo.getFireDate() != null, ProTask::getFireDate, bo.getFireDate());
        lqw.eq(bo.getDemandDate() != null, ProTask::getDemandDate, bo.getDemandDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ProTask::getStatus, bo.getStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getSubjectId()), ProTask::getSubjectId, bo.getSubjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadUser()), ProTask::getHeadUser, bo.getHeadUser());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyUser()), ProTask::getCopyUser, bo.getCopyUser());
        return lqw;
    }


    /**
     * 新增任务管理
     */
    @Override
    public Boolean insertByBo(ProFile bo) {
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }


    /**
     * 修改任务管理
     */
    @Override
    public Boolean updateByBo(ProFile bo) {
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 批量删除任务管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ProFile> queryListByKeyAndRefId(String key, Long refId) {
        ProFile file = new ProFile();
        file.setVkey(key);
        file.setRefId(refId);
        List<ProFile> proFiles = queryList(file);
        return proFiles;
    }

}
